#include <math.h>
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;

class Figure {
public:
    virtual string getName() const = 0;
    virtual double getArea() const = 0;
};

void display(Figure & fig) {
    cout << fig.getName() 
         << "的面积是:" 
         << fig.getArea() << endl ;
}

class Rectangle//矩形
: public Figure
{
public:
    Rectangle(double len,double wid)
    : _len(len)
    , _wid(wid)
    {}

    string getName() const override{
        return "矩形";
    }

    double getArea() const override{
        return _len * _wid;
    }
private:
    double _len;
    double _wid;
};

class Circle
: public Figure
{
public:
    Circle(double rad)
    : _radius(rad)
    {}

    string getName() const override{
        return "圆形";
    }

    double getArea() const override{
        return PI * _radius * _radius;
    }

private:
    //将运行时常量转换成编译时常量
    static constexpr double PI = 3.14;
    double _radius;
};

class Triangle
: public Figure
{
public:
    Triangle(double a,double b,double c)
    : _a(a)
    , _b(b)
    , _c(c)
    {}

    string getName() const override{
        return "三角形";
    }

    double getArea() const override{
        double p = (_a + _b + _c)/2;
        return sqrt(p * (p - _a) * (p - _b) * (p - _c));
    }
private:
    double _a;
    double _b;
    double _c;
};


void test0() {
    Rectangle r(10, 20);
    Circle c(2);
    Triangle t(3, 4, 5);
    display(r);
    display(c);
    display(t);
}

int main(void){
    test0();
    return 0;
}
